Ext.onReady( function() {
	var carousel = new Ext.ux.Carousel('examples-carousel', {
		itemSelector : 'div.carousel-item',
		interval : 1,
		autoPlay : false,
		transitionType : 'fade',
		hideNavigation : true,
		freezeOnHover : true
	});

	var shortcuts = Ext.query('#carousel-navigation-shortcuts > img');
	
	//注册鼠标事件
	regeditIconEven(shortcuts);
	
	//响应点击事件
	Ext.fly('carousel-navigation-shortcuts').on('click', function() {
		var index = shortcuts.indexOf(this.dom);
		carousel.pause();
		carousel.setSlide(index);
	}, null, {
		delegate : 'img'
	});

	//移动浮标...
	function moveArrow(slide, index) {
			var shortcut = Ext.get(shortcuts[index]), arrow = Ext.get('carousel-navigation-arrow');
			var left = shortcut.getOffsetsTo('carousel-navigation')[0]
					+ (shortcut.getWidth() / 2) - (arrow.getWidth() / 2);
			arrow.setLeft(left + 'px');
	}
	
	///**移动被激活的快捷方式的浮标..*/
	carousel.on('change', function(slide, index) {
		moveArrow(slide, index);
	});

	Ext.fly('carousel-navigation-buttons').on('click', function() {
		switch (this.dom.id) {
		case 'nav-next':
			carousel.pause().next();
			//Ext.fly('nav-pause').radioClass('nav-hide');
			break;
		case 'nav-prev':
			carousel.pause().prev();
			//Ext.fly('nav-pause').radioClass('nav-hide');
			break;
		}
	}, null, {
		delegate : 'img'
	});
	if (Ext.isIE6) {
		//force reflow of arrow in ie6
		Ext.fly('carousel-navigation-arrow').setStyle('display', 'none')
				.setStyle('display', 'block');
	}
	moveArrow(null,0); //默认移动到第一个快捷方式
	Ext.Ajax.on('beforerequest', showLoading, this);
	Ext.Ajax.on('requestcomplete', endLoading, this);
	Ext.Ajax.on('requestexception', errorLoading, this);
	Ext.fly('srcUrlInput').focus();
	// 输入框加入键盘监听事件,监听Enter键输入.
	Ext.fly('srcUrlInput').on('keydown',function(e){
        if (e.getKey()==13){
        	tinyUrl();
        }
	});
});

/** 隐藏消息窗口 */
function hideMessagePanel(animate) {
	var msgPanel = Ext.fly('message');
	//msgPanel.ghost();
	msgPanel.dom.style.visibility = 'hidden';
}

var closeHTML = '&nbsp;<a id="closeMsg" href="#" title="点击关闭" onclick="hideMessagePanel(true)">[×]</a> ';

var msgTask = new Ext.util.DelayedTask(function(){
	var msgPanel = Ext.fly('message');
	if(msgPanel.isVisible()){
		msgPanel.fadeOut({ duration: 1.5}) ;
	}
});

/** 显示消息 */
function showMsg(text, type) {
	var msgPanel = Ext.fly('message');
	msgPanel.dom.innerHTML = text + closeHTML;
	msgPanel.dom.style.visibility = 'visible';
	if(type == 'moment'){
		msgTask.delay(4000); 
	}else if (type == 'long'){
		msgTask.delay(8000); 
	}
}

/** 复制到剪贴板 */
function copyToClipboard(txt) {
	if (window.clipboardData) {
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", txt);
	} else if (navigator.userAgent.indexOf("Opera") != -1) {
		window.location = txt;
	} else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager
					.enablePrivilege("UniversalXPConnect");
		} catch (e) {
			alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
		}
		var clip = Components.classes['@mozilla.org/widget/clipboard;1']
				.createInstance(Components.interfaces.nsIClipboard);
		if (!clip)
			return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1']
				.createInstance(Components.interfaces.nsITransferable);
		if (!trans)
			return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"]
				.createInstance(Components.interfaces.nsISupportsString);
		var copytext = txt;
		str.data = copytext;
		trans.setTransferData("text/unicode", str, copytext.length * 2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip)
			return false;
		clip.setData(trans, null, clipid.kGlobalClipboard);
	}
}

/**把url复制到剪贴板*/
function copyToClip(url) {
	var suourl = Ext.fly('suourl');
	copyToClipboard(url);
	showMsg('缩短后的网址已经复制到您的剪贴板上了.', 'moment');
}

/** 缩址 */
function tinyUrl(){
	var input = Ext.fly('srcUrlInput');
	var srcUrl = input.getValue();
	if(!srcUrl){
		showMsg('源地址不能为空!', 'moment');
		return ;
	}
	Ext.Ajax.request({
		url: '/api?url='+ srcUrl,
		success: function(response, opts) {
			if(response && response.responseText && response.responseText.length<100){
				var url = response.responseText;
				var suourl = Ext.fly('suourl');
				
				var type = getShowType();
				if(type == 1 ){
					suourl.dom.innerHTML = '<a href="'+ url + '" target="_blank" >' + url + '</a>'; 	
				}else if(type == 2){
					suourl.dom.innerHTML = '[URL]'+ url + '[/URL]' ; 	
				}else if(type == 3){
					suourl.dom.textContent = '<a href="'+ url + '">' + url + '</a>';
				}
				suourl.frame(); //特效
				var auto = Ext.fly('autoCopyBox');
				if(auto!=null && auto.dom.checked){
				  copyToClip(url);
				}
			}else{
				showMsg('生成短链接失败!');
			}
		    //console.log(response.responseText);
		},
		failure: function(response, opts) {
			if(response && response.responseText){
				
			}else{
				showMsg('生成短链接失败!');				
			}
		   //console.log('server-side failure' + response.status);
		}
	});
}

/**
 * 底部图标鼠标飘过事件
 */ 
function imgOver(num){
	var el = Ext.fly('img'+num);
	el.frame();
}

/**
 * 底部图标注册鼠标事件
 */
function regeditIconEven( shortcuts ){
	for( i=0 ; i< shortcuts.length ;i++){
		var node = Ext.fly(shortcuts[i]);
		node.on('mouseleave',function(e){
			e.preventDefault();
			Ext.fly(e.getTarget()).frame("D1D1D1");
		});
		node.on('click',function(e){
			e.preventDefault();
			Ext.fly(e.getTarget()).frame();
		});
	}
}

var runner = new Ext.util.TaskRunner();
var waitTask = {
	run: function(){
			var suourl = Ext.fly('suourl');
			suourl.dom.innerHTML = suourl.dom.innerHTML +'.';
		},
	interval: 300 
}

/**
 *展示正在读取数据的状态
 */ 
function showLoading(){
	var suourl = Ext.fly('suourl');
	suourl.dom.innerHTML = '缩址中.';
	runner.start(waitTask);
}

/**
 * 结束展示正在读取数据..
 */ 
function endLoading(){
	runner.stopAll();
}

/**
 * 结束展示,读取数据錯誤..
 */
function errorLoading(){
	showMsg('缩址发生错误.. >_<', 'moment');
	runner.stopAll();
}

/**
 * 取得展现类型, 1,普通URL  2,UBB代码 3,HTML代码
 */
function getShowType(){
	var sap = Ext.get('sampleType');
	if( sap.dom.checked ){
		return 1;
	}
	
	var ubb = Ext.get('ubbType');
	if( ubb.dom.checked ){
		return 2;
	}
	
	var html = Ext.get('htmlType');
	if( html.dom.checked ){
		return 3;
	}
	return 1;
}
